/*****************************************************************
Mix3D WebGL Game Engine
Copyright (c) fantasy ( fantasy.x2x@gmail.com )
Listens:
Distributed under the Boost Software License, Version 1.0.
http://www.boost.org/LICENSE_1_0.txt
 ****************************************************************/
/**
 * @Desc    : color
 * @Author  : fantasy(fantasy.x2x@gmail.com)
 */
//////////////////////////////////////////////////////////////////
if(typeof(Mix3D) === 'undefined')
{
  Mix3D = {};
};
(function(Mix3D){
//////////////////////////////////////////////////////////////////
Mix3D.Color = function(hex)
{
  //R:0, G:1, B:2
  Mix3D.FloatArray.call(this, 3);
  if(hex!==undefined)this.setHex(hex);
  return this;
};

Mix3D.Color.prototype.constructor = Mix3D.Color;

Mix3D.Color.prototype.setRGB = function( r, g, b)
{
  this.FA[0] = r;
  this.FA[1] = g;
  this.FA[2] = b;
  return this;
};

Mix3D.Color.prototype.setHSV = function( h, s, v)
{
  if(v===0)
  {
    this.FA[0]=0; this.FA[1] = 0; this.FA[2]=0;
  }
  else
  {
    var i, f, p, q, t;
    i=Math.floor(h*6);
    f=(h*6)-i;
    p=v*(1-s);
    q=v*(1-(s*f));
    t=v*(1-(s*(1-f)));
    switch(i)
    {
      case 1:this.FA[0]=q;this.FA[1]=v;this.FA[2]=p;break;
      case 2:this.FA[0]=p;this.FA[1]=v;this.FA[2]=t;break;
      case 3:this.FA[0]=p;this.FA[1]=q;this.FA[2]=v;break;
      case 4:this.FA[0]=t;this.FA[1]=p;this.FA[2]=v;break;
      case 5:this.FA[0]=v;this.FA[1]=p;this.FA[2]=q;break;
      default:this.FA[0]=v;this.FA[1]=t;this.FA[2]=p;break;
    }
  }
  return this;
};

Mix3D.Color.prototype.setHex = function(hex)
{
  hex = Math.floor(hex);
  this.FA[0] = (hex>>16&255)/255;
  this.FA[1] = (hex>>8&255)/255;
  this.FA[2] = (hex&255)/255;
  return this;
};

Mix3D.Color.prototype.lerpSelf = function(color, alpha)
{
  this.FA[0] +=(color.FA[0]-this.FA[0])*alpha;
  this.FA[1] +=(color.FA[1]-this.FA[1])*alpha;
  this.FA[2] +=(color.FA[2]-this.FA[2])*alpha;
  return this;
};

Mix3D.Color.prototype.getHex = function()
{
  return (Math.floor(this.FA[0]*255)<<16)^(Math.floor(this.FA[1]*255)<<8)^(Math.floor(this.FA[2]*255));
};

Mix3D.Color.prototype.getContextStyle = function()
{
  return 'rgb(' + Math.floor(this.FA[0]*255) + ',' + Math.floor(this.FA[1]*255) + ',' + Math.floor(this.FA[2]*255) + ')';
};

Mix3D.Color.prototype.copyGammaToLinear = function(color)
{
  this.FA[0] = color.FA[0]*color.FA[0]; //R
  this.FA[1] = color.FA[1]*color.FA[1]; //G
  this.FA[2] = color.FA[2]*color.FA[2]; //B
  return this;
};

Mix3D.Color.prototype.copyLinearToGamma = function(color)
{
  this.FA[0] = Math.sqrt(color.FA[0]);
  this.FA[1] = Math.sqrt(color.FA[1]);
  this.FA[2] = Math.sqrt(color.FA[2]);
  return this;
};

Mix3D.Color.prototype.convertGammaToLinear = function()
{
  var r=this.FA[0], g=this.FA[1], b=this.FA[2];
  this.FA[0] = r*r;
  this.FA[1] = g*g;
  this.FA[2] = b*b;
  return this;
};

Mix3D.Color.prototype.convertLinearToGamma = function()
{
  this.FA[0] = Math.sqrt(this.FA[0]);
  this.FA[1] = Math.sqrt(this.FA[1]);
  this.Fa[2] = Math.sqrt(this.FA[2]);
  return this;
};

Mix3D.Color.prototype.clone = function()
{
  return new Mix3D.Color().copy(this);
};

Mix3D.protoCopy(Mix3D.FloatArray, Mix3D.Color);
//////////////////////////////////////////////////////////////////
})(Mix3D);// end of function(Mix3D)